<template>
  <div>
    <h1>{{ id ? "编辑" : "新建" }}英雄</h1>
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-tabs value="skills" type="border-card" v-model="activeName">
        <!-- 基础信息 start -->
        <el-tab-pane label="基础信息" name="info">
          <el-row>
            <el-col :span="12">
              <el-form-item label="名称">
                <el-input v-model="model.name"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="称号">
                <el-input v-model="model.title"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item label="头像">
            <el-upload
              class="avatar-uploader"
              name="uploadfile"
              :action="$http.defaults.baseURL + '/upload'"
              :show-file-list="false"
              :on-success="afterUpload"
            >
              <img v-if="model.avatar" :src="model.avatar" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <!-- 英雄类型 -->
          <el-form-item label="类型">
            <el-select v-model="model.categories" multiple>
              <el-option
                v-for="item of categories"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>

          <!-- 星级评分数据 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="难度">
                <el-rate
                  style="margin-top: 0.7rem"
                  v-model="model.scores.difficult"
                  :max="10"
                  show-score
                ></el-rate>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="技能">
                <el-rate
                  style="margin-top: 0.7rem"
                  v-model="model.scores.skills"
                  :max="10"
                  show-score
                ></el-rate>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="攻击">
                <el-rate
                  style="margin-top: 0.7rem"
                  v-model="model.scores.attack"
                  :max="10"
                  show-score
                ></el-rate>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="生存">
                <el-rate
                  style="margin-top: 0.7rem"
                  v-model="model.scores.survive"
                  :max="10"
                  show-score
                ></el-rate>
              </el-form-item>
            </el-col>
          </el-row>

          <!-- 装备：顺风出装 逆风出装 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="顺风出装">
                <el-select v-model="model.items1" multiple>
                  <el-option
                    v-for="item of items"
                    :key="item._id"
                    :label="item.name"
                    :value="item._id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="逆风出装">
                <el-select v-model="model.items2" multiple>
                  <el-option
                    v-for="item of items"
                    :key="item._id"
                    :label="item.name"
                    :value="item._id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item label="使用技巧">
            <el-input type="textarea" v-model="model.usageTips"></el-input>
          </el-form-item>

          <el-form-item label="对抗技巧">
            <el-input type="textarea" v-model="model.battleTips"></el-input>
          </el-form-item>

          <el-form-item label="团战思路">
            <el-input type="textarea" v-model="model.teamTips"></el-input>
          </el-form-item>
        </el-tab-pane>
        <!--  基础信息 end -->

        <!-- 技能 start -->
        <el-tab-pane label="技能" name="skills">
          <el-button size="small" @click="addSkill">
            <i class="el-icon-plus"></i>添加技能
          </el-button>
          <el-row type="flex" style="flex-wrap: wrap">
            <el-col :md="12" v-for="(skill, i) in model.skills" :key="i">
              <el-form-item label="名称">
                <el-input v-model="skill.name"></el-input>
              </el-form-item>

              <el-form-item label="图标">
                <el-upload
                  class="avatar-uploader"
                  name="uploadfile"
                  :action="uploadUrl"
                  :show-file-list="false"
                  :on-success="(res) => $set(skill, 'icon', res.url)"
                >
                  <img v-if="skill.icon" :src="skill.icon" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>

              <el-form-item label="冷却值">
                <el-row
                  :gutter="20"
                  type="flex"
                  justify="between"
                  style="flex-wrap: wrap"
                >
                  <!-- v-for循环数值从1开始 -->
                  <el-col
                    :sm="12"
                    :lg="8"
                    :xl="4"
                    v-for="i in skill.cold.length"
                    :key="i"
                  >
                    <el-input-number
                      controls-position="right"
                      size="small"
                      :precision="1"
                      :step="0.1"
                      :max="100"
                      v-model="skill.cold[i - 1]"
                    ></el-input-number>
                  </el-col>
                </el-row>
              </el-form-item>

              <el-form-item label="消耗值">
                <el-input-number
                  controls-position="right"
                  size="small"
                  :step="10"
                  :max="100"
                  v-model="skill.consume"
                ></el-input-number>
              </el-form-item>

              <el-form-item label="描述">
                <el-input
                  v-model="skill.description"
                  type="textarea"
                ></el-input>
              </el-form-item>

              <el-form-item label="小提示">
                <el-input v-model="skill.tips" type="textarea"></el-input>
              </el-form-item>

              <el-form-item>
                <el-button
                  size="small"
                  type="danger"
                  @click="model.skills.splice(i, 1)"
                  >删除</el-button
                >
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        <!-- 技能 end -->
      </el-tabs>

      <el-button
        type="primary"
        native-type="submit"
        style="margin-top: 1rem; margin-left: 4rem"
        >保存</el-button
      >
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    id: {}
  },
  data () {
    return {
      activeName: 'info',
      categories: [], // 英雄分类数据
      items: [], // 装备类型数据
      // 后台返回的数据
      model: {
        name: '',
        avatar: '',
        scores: {},
        skills: [] // 添加技能
      }
    }
  },
  created () {
    // 自动执行方法获取 编辑分类的数据
    // 如果id存在，才执行fetch()方法
    this.id && this.fetch()
    this.fetchCategories() // 分类
    this.fetchItems() // 顺风出装物品
  },
  methods: {
    /** 保存数据 */
    async save () {
      let res
      if (this.id) { // id存在则put修改
        res = await this.$http.put(`rest/heros/${this.id}`, this.model)
      } else { // 没有id则新增
        res = await this.$http.post('rest/heros', this.model)
      }
      console.log(res)
      this.$router.push('/heros/list').catch(err => { console.log(err) })
      this.$message({
        type: 'success',
        message: '保存成功'
      })
    },
    /** id存在时编辑数据 */
    async fetch () {
      const res = await this.$http.get(`rest/heros/${this.id}`)
      console.log('编辑数据=>', res)
      this.model = res.data
      // this.model = Object.assign({}, this.model, res.data)
    },
    /** 获取分类数据 categories */
    async fetchCategories () {
      const res = await this.$http.get(`rest/categories`)
      // console.log(res)
      this.categories = res.data.items
    },
    /** 获取装备(物品) */
    async fetchItems () {
      const res = await this.$http.get(`rest/items`)
      console.log('装备=>', res)
      this.items = res.data.items
    },
    /** 基础信息上传头像 */
    afterUpload (res) {
      // model中已经有需要用到的数据了，可以响应式
      this.model.avatar = res.url
      console.log(res)
    },
    // ----------------------技能
    addSkill () {
      this.model.skills.push({
        cold: new Array(6).fill(0),
        consume: 0
      })
    },
    skillIconSuccess (res, avatar, fileList) {
      // console.log('skillIcon=>', res)
      // console.log('skillIconavatar', avatar)
      // console.log('skillIconList', fileList)

    }
  }
}
</script>

<style scoped>
.avatar-uploader-icon {
  width: 5rem;
  height: 5rem;
  line-height: 5rem;
}
.avatar {
  width: 5rem;
  height: 5rem;
}

.el-input-number--small {
  width: 100px;
  margin-right: 10px;
}
</style>
